//计算零售
/**
 * data 全部不分类数据
 * item45List 45的数据
 * itemNoList 非45的数据
 * */
function calculatedRetail(data, item45List, itemNo45List) {
    //合并数组
    var retailBAT = [];
    //汇总数据
    var itemRetailList = [];
    //45数据库中的数据
    var itemRetail45 = [];
    //非45数据库中的数据
    var itemRetailNo45 = [];
    //获取数据库中的数据
    if (!isNull(data[0].JSON_RETAIL_LIST)) {
        //从第0个获取数据
        var oldRetailList;
        if (typeof data[0].JSON_RETAIL_LIST == 'string'){
            oldRetailList = JSON.parse(data[0].JSON_RETAIL_LIST);
        }else {
            oldRetailList = data[0].JSON_RETAIL_LIST;
        }
        //获取itemNur
        var itemNbr = data[0].ITEM_NBR;
        //获取箱包数
        var size = data[0].VNPK_QTY;
        //获取汇率
        var rate = data[0].TAX_RATE;


        var now = new Date();

        //旧数据45数组
        var old45 = [];
        //旧数据非45数组
        var oldNo45 = [];
        //旧数据45BP数组
        var old45BP = [];
        //旧数据45非BP数组
        var old45NoBP = [];
        //旧数据非45BP数组
        var oldNo45BP = [];
        //旧数据非45非BP数组
        var oldNo45NoBP = [];
        if (isNotEmpty(oldRetailList)){
            for (var i41 = 0; i41 < oldRetailList.length; i41++) {
                //如果旧数据库中的BP的结束时间不为2049，则跳过，此条BP数据以TR为准

                var oldRetail = oldRetailList[i41];
                if (oldRetail.EXPIRATION_DATE !== '2049-12-31'
                    && oldRetail.PRICE_TYPE === 'BP'
                    && Date.parse(oldRetail.EFFECTIVE_DATE) <now) {
                    continue;
                }
                var old = {
                    itemNbrT: itemNbr,
                    effDate: oldRetail.EFFECTIVE_DATE,
                    expDate: oldRetail.EXPIRATION_DATE,
                    priceType: oldRetail.PRICE_TYPE,
                    type: 'old',
                    priceTrait: oldRetail.PRICE_TRAIT,
                    retail: oldRetail.CUST_BASE_RTL_AMT,
                    size: size,
                    rate: rate,
                    comment: '',
                };

                //45的数据库数据
                if (old.priceTrait == 45) {
                    //将数据库数据添加至旧数据45数组中
                    old45.push(old);
                    if (old.priceType === 'BP') {
                        //旧数据45BP数组
                        old45BP.push(old);
                    } else {
                        //旧数据45非BP数组
                        old45NoBP.push(old);
                    }
                } else {
                    //非45数据库数据
                    oldNo45.push(old);

                    if (old.priceType === 'SB' || old.priceType ==='BP') {
                        //旧数据非45BP数组
                        oldNo45BP.push(old);
                    } else {
                        //旧数据非45非BP数组
                        oldNo45NoBP.push(old);
                    }
                }
            }
        }

        //新数据45数组
        var new45 = [];
        //新数据非45数组
        var newNo45 = [];
        //新数据45BP数组
        var new45BP = [];
        //新数据45非BP数组
        var new45NoBP = [];
        //新数据非45BP数组
        var newNo45BP = [];
        //新数据非45非BP数组
        var newNo45NoBP = [];

        //直接进行计算的数组
        var calculateDirectly = new Map();
        var calculateDirectlyNo45 = new Map();
        if (isNotEmpty(data)){
            for (var i5 = 0; i5 < data.length; i5++) {
                var re = data[i5];
                var retail = {
                    itemNbrT: re.ITEM_NBR,
                    effDate: re.NEW_RETAIL_EFF_DATE,
                    expDate: re.NEW_RETAIL_EXP_DATE,
                    priceType: re.RETAIL_TYPE,
                    type: 'new',
                    priceTrait: re.TRAIT_STORE_NBR,
                    retail: re.NEW_RETAIL,
                    size: size,
                    rate: rate,
                    comment: '',
                    id: re.ID,
                };
                if (retail.priceTrait == 45) {
                    //将数据库数据添加至新数据45数组中
                    new45.push(retail);
                    if (retail.priceType === 'BP') {
                        //新数据45BP数组
                        new45BP.push(retail);
                    } else {
                        //新数据45非BP数组
                        new45NoBP.push(retail);
                    }
                } else {
                    //非45数据库数据
                    newNo45.push(retail);

                    if (retail.priceType === 'SB' || retail.priceType ==='BP') {
                        //新数据非45BP数组
                        newNo45BP.push(retail);
                    } else {
                        //新数据非45非BP数组
                        newNo45NoBP.push(retail);
                    }
                }
            }
        }

        //如果新建数组存在新的BP数据
        if (isNotEmpty(new45BP)) {
            //对新建的45BP数据进行按开始时间排序
            sortArrayByEffDate(new45BP);
            //如果数据库中存在旧的BP数据
            if (isNotEmpty(old45BP)) {
                //对数据库中的45BP数据进行排序
                // sortArrayByEffDate(old45BP);
                //循环数据库中的45BP数据，循环新建的45BP数据，删除过期时间大于今天的数据，修改之前的数据
                var concat1 = old45BP.concat(new45BP);
                sortArrayByEffDate(concat1);
                calculateDate(concat1,"retail-new");
                ArrayDeAndPutIntoMap(concat1,calculateDirectly);
                //有新建BP则清空数据库中所有非45数据
                oldNo45 = [];
                oldNo45BP = [];
                oldNo45NoBP = [];

            } else {
                //如果不存在BP则舍弃数据库中所有45非45数据
                old45 = [];
                old45BP = [];
                old45NoBP = [];
                oldNo45 = [];
                oldNo45BP = [];
                oldNo45NoBP = [];
                ArrayDeAndPutIntoMap(new45BP,calculateDirectly);
            }

        }
        //不存在新建立的45新数据则分为几种情况
        //1、存在45非BP数据
        if (isNotEmpty(new45NoBP)) {
            //存在45非BP数据，则取数据库中更改数据库中已存在的非BP数据，并且最后的结束时间为新建BP的结束时间
            if (isNotEmpty(old45NoBP)) {
                //CSRL中去除priceType = AR/TR 结束时间小于等于新AR/TR开始时间的数据
                for (var new45NB1 = 0; new45NB1 < new45NoBP.length; new45NB1++) {
                    var newNo45BPEl = new45NoBP[new45NB1];
                    for (var n45N1 = 0; n45N1 < old45NoBP.length; n45N1++) {
                        var n45El = old45NoBP[n45N1];
                        if (n45El.expDate<=newNo45BPEl.effDate && n45El.priceType === newNo45BPEl.priceType){
                            old45NoBP.splice(n45N1,1);
                            n45N1--;
                        }
                    }
                }

                //如果数据库中存在45非BP的数据
                //按时间段，更改结束时间为新建非BP的开始时间，最末尾的时间段为新建非BP的结束时间
                var c45n1 = old45NoBP.concat(new45NoBP);

                if (isNotEmpty(old45BP)){
                    var concat3 = c45n1.concat(old45BP);
                    sortArrayByEffDate(concat3);
                    calculateDate(concat3,'retail-NoBP');
                    ArrayDeAndPutIntoMap(concat3,calculateDirectly);
                }else {
                    sortArrayByEffDate(c45n1);
                    calculateDate(c45n1,'retail-NoBP');
                    ArrayDeAndPutIntoMap(c45n1,calculateDirectly);
                }

            } else {
                if (isNotEmpty(old45BP)){
                    for (var number = 0; number < old45BP.length; number++) {
                        var c45n2 = old45BP.concat(new45NoBP);
                        sortArrayByEffDate(c45n2);
                        calculateDate(c45n2,'retail-NoBP');
                        ArrayDeAndPutIntoMap(c45n2,calculateDirectly);
                    }
                }
                //如果数据库中不存在45的非BP数据，则直接进行添加计算
                // calculateDirectly.push.apply(calculateDirectly,new45NoBP);
                ArrayDeAndPutIntoMap(new45NoBP,calculateDirectly);
            }
        }
        //2、不存在45非BP数据就开始计算非45BP数据
        if (isNotEmpty(newNo45BP)) {
            //如果数据库中存在非45BP数据，则找出对应的trait号的BP数据
            if (isNotEmpty(oldNo45BP)) {
                var tmpNo45bp = [];
                for (var no1 = 0; no1 < newNo45BP.length; no1++) {
                    var no45BPElement = newNo45BP[no1];
                    for (var i9 = 0; i9 < oldNo45BP.length; i9++) {
                        var arr1Element = oldNo45BP[i9];
                        if (no45BPElement.priceTrait === arr1Element.priceTrait){
                            //如果有新进非45BP则需要清空该trait号下的非BP数据
                            tmpNo45bp.push(arr1Element)
                        }

                    }
                }
                var cn45B1 = newNo45BP.concat(tmpNo45bp);

                var groupArr2 = groupBy(cn45B1, function (item) {
                    return [item.priceTrait];
                });

                for (var no45bp2 = 0; no45bp2 < groupArr2.length; no45bp2++) {
                    var no45elm2 = groupArr2[no45bp2];
                    //如果有新进非45BP则需要清空该trait号下的非BP数据
                    if (isNotEmpty(oldNo45NoBP)){
                        for (var j = 0; j < oldNo45NoBP.length; j++) {
                            if (oldNo45NoBP[j].priceTrait === no45elm2[0].priceTrait){
                                oldNo45NoBP.splice(j,1);
                                j--;
                            }
                        }
                    }
                    sortArrayByEffDate(no45elm2);
                    calculateDate(no45elm2,'retail-NoBP');
                    ArrayDeAndPutIntoMap(no45elm2,calculateDirectlyNo45);
                }

            } else {
                //如果不存在则直接添加数据，可能原因可能因为前面步骤已添加了45BP数据导致数据清空
                ArrayDeAndPutIntoMap(newNo45BP,calculateDirectlyNo45);
            }
        }
        //3、不存在非45BP数据开始计算非45非BP数据
        if (isNotEmpty(newNo45NoBP)) {
            if (isNotEmpty(oldNo45NoBP)){
                var cn451 = newNo45NoBP.concat(oldNo45NoBP);
                var ga2 = groupBy(cn451, function (item) {
                    return [item.priceTrait];
                });
                for (var i12 = 0; i12 < ga2.length; i12++) {
                    var g2 = ga2[i12];
                    var groupArr1 = groupBy(g2, function (item) {
                        return [item.priceType];
                    });
                    for (var j1 = 0; j1 < groupArr1.length; j1++) {
                        var g1Type = groupArr1[j1];
                        //如果数据库中的非45BP数据存在,则删除对应trait号下的非BP数据
                        sortArrayByEffDate(g1Type);
                        calculateDate(g1Type,'retail-NoBP');
                        ArrayDeAndPutIntoMap(g1Type,calculateDirectlyNo45);
                    }

                }
            }else {
                if (isNotEmpty(oldNo45BP)) {
                    var cn45B11 = newNo45NoBP.concat(oldNo45BP);

                    var groupArr22 = groupBy(cn45B11, function (item) {
                        return [item.priceTrait];
                    });

                    for (var i91 = 0; i91 < groupArr22.length; i91++) {
                        var arr1Element11 = groupArr22[i91];
                        sortArrayByEffDate(arr1Element11);
                        calculateDate(arr1Element11,'retail-NoBP');
                        ArrayDeAndPutIntoMap(arr1Element11,calculateDirectlyNo45);
                    }

                }else if (isNotEmpty(old45BP)){
                    var concat2 = newNo45NoBP.concat(old45BP);
                    sortArrayByEffDate(concat2);
                    calculateDate(concat2,'retail-NoBP');
                    ArrayDeAndPutIntoMap(concat2,calculateDirectlyNo45);
                }
                ArrayDeAndPutIntoMap(newNo45NoBP,calculateDirectlyNo45);
            }
        }else if (!isNotEmpty(newNo45NoBP) && isNotEmpty(oldNo45NoBP)){
            ArrayDeAndPutIntoMap(oldNo45NoBP,calculateDirectlyNo45);
        }
        var values = calculateDirectly.values();
        var values1 = calculateDirectlyNo45.values();
        var concat = values.concat(values1);
        if (isNotEmpty(concat)) {
            for (var i = 0; i < concat.length; i++) {
                if (concat[i].effDate == concat[i].expDate) {
                    concat.splice(concat[i], 1);
                    i--;
                }
            }
        }

        sortArrayByEffDate(concat);
        return concat;
    }


}
